package com.dongdongshop.controller;

import com.dk.data.R;
import com.dongdongshop.pojo.User;
import com.dongdongshop.service.UserService;
import com.dongdongshop.shiroutil.ShiroUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static com.dk.enums.ResultEnum.*;

//注册登录
@Controller
@RequestMapping("user")
public class UserController {

    @DubboReference
    UserService us;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    RocketMQTemplate rocketMQTemplate;

    //跳转注册页面
    @RequestMapping("tiao")
    public String tiao(){
        return "register";
    }

    //跳转登录
    @RequestMapping("deng")
    public String deng(){
        return "login";
    }


    //判断用户名是否存在
    @RequestMapping("byusername")
    @ResponseBody
    public R byusername(String username){
        User u = us.byuser(username);
        if(u==null){
            return R.error();
        }
        return R.ok().setData(u);

    }


    //发送手机号获取验证码
    @RequestMapping("code")
    @ResponseBody
    public R  code(String phone){
        rocketMQTemplate.convertAndSend("code_phone",phone);//生产者发送消息
        return R.ok();
    }


    //注册
    @RequestMapping("login")
    @ResponseBody
    public R login(User user,String code){
        String phone = user.getPhone();
        String s = (String)redisTemplate.boundValueOps(phone).get();//正确验证码
        if(!s.equals(code)){//判断验证码是否相同
            return R.error();
        }
        String salt = ShiroUtils.generateSalt(6);//盐
        String md5 = ShiroUtils.encryptPassword("MD5", user.getPassword(), salt, 3);//MD5算法，要加密的字段pwd,盐值加密，撒盐3次
        user.setPassword(md5);//密码撒盐加密
        user.setSalt(salt);
        us.login(user);
        return R.ok();
    }



    //登录
    @RequestMapping("denglu")
    @ResponseBody
    public R denglu(String username, String password){

        stringRedisTemplate.delete("username");//登陆之前先删除缓存

        Subject subject= SecurityUtils.getSubject();//获取到subject
        UsernamePasswordToken token=new UsernamePasswordToken(username,password);//封装token到LoginRealm类

        try {
            subject.login(token);
        }catch (UnknownAccountException e){
            return R.response(USER_NULL);//用户名不存在
        }catch (IncorrectCredentialsException e){
            return R.response(PASS_ERROR);//密码错误
        }
        String key ="username";
        stringRedisTemplate.boundValueOps(key).set(username);//登录将用户名存入缓存
        return R.ok();//登录成功
    }



}
